package com.wicket.okrframework.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrframework.base.service.MOrganizationService;
import com.wicket.okrframework.base.service.dto.req.BatchQueryOrgReq;
import com.wicket.okrframework.base.service.dto.req.QueryOrgDetailReq;
import com.wicket.okrframework.base.service.dto.req.QueryOrgListReq;
import com.wicket.okrframework.common.annotations.BanAuto;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.dal.mapper.mbg.OrganizationMapper;
import com.wicket.okrframework.dal.po.mbg.Organization;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.wicket.okrframework.base.service.dto.req.ImplementSpinLockCheckOrgReq;
import java.util.stream.Collectors;
import com.wicket.okrframework.base.service.dto.req.BatchDeleteOrgTestDataReq;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOrganizationServiceImpl
        extends ServiceImpl<OrganizationMapper, Organization>
        implements MOrganizationService {

    @Autowired
    OrganizationMapper organizationMapper;

    /**
   * 1-3-12查询组织详情[201]
   * gen by moon at 4/17/2023, 11:50:50 PM
   */
 @Trace(operationName = "1-3-12查询组织详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  Organization  queryOrgDetail(QueryOrgDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Organization> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                Organization retData = retList.get(0);
                
                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 1-3-12查询组织列表[200]
   * gen by moon at 10/16/2022, 11:08:24 AM
   */
 @Trace(operationName = "1-3-12查询组织列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<Organization>  queryOrgList(QueryOrgListReq reqDto) {
     
        try {
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Organization.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Organization.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Organization.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<Organization>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-3-12批量查询组织列表[1346]
   * gen by moon at 10/16/2022, 11:08:25 AM
   */
 @Trace(operationName = "1-3-12批量查询组织列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<Organization>  batchQueryOrg(BatchQueryOrgReq reqDto) {
     
        try {
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            
            if(reqDto.getOrgList()==null || reqDto.getOrgList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("organization_id", reqDto.getOrgList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Organization.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Organization.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Organization.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<Organization>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-3-12删除组织[198]
   * gen by moon at 10/16/2022, 11:08:23 AM
   */
 @Trace(operationName = "1-3-12删除组织")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteOrg(String organizationId ) {
     
      return super.removeById(organizationId);
  }

    /**
   * 1-3-12修改组织[197]
   * gen by moon at 9/6/2022, 10:42:05 PM
   */
 @Trace(operationName = "1-3-12修改组织")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
 @BanAuto
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateOrg(Organization organization) {
     if(organization.getReasonOfUsingLock()!=null && organization.getReasonOfUsingLock().equals("---")){
         organization.setReasonOfUsingLock(null);
     }
     organization.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
organization.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(organization);
  }

    /**
   * 1-3-12新增组织[196]
   * gen by moon at 10/16/2022, 11:08:23 AM
   */
 @Trace(operationName = "1-3-12新增组织")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addOrg(Organization organization) {
     organization.setOrganizationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
organization.setArchivngType("NOT_ARCHIVE");
organization.setIsArchive("FALSE");
organization.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
organization.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(organization!=null && organization.getSubjectLifeCycle()!=null &&  organization.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            organization.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
organization.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(organization!=null && organization.getSubjectLifeCycle()!=null && organization.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             organization.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
organization.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
organization.setIsValid("FALSE");
     if(super.save(organization)){
        return organization.getOrganizationId();
     }else {
        return "-1";
     }
    
  }
/**
   * 1-3-12执行悬停查组织（悬停）[4304]
   * gen by moon at 8/29/2022, 11:59:02 PM
   */
 @Trace(operationName = "1-3-12执行悬停查组织（悬停）")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
 @BanAuto
  public  Organization  implementSpinLockCheckOrg(ImplementSpinLockCheckOrgReq reqDto) {
     try {
         long count =500;
         while(true){
             if(count==0){
                 break;
             }
             reqDto.setIsValid("FALSE");
             QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
             Map<String, Object> fieldInfoMap = null;
             fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
             CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
             queryWrapper.orderByDesc("create_time");
             if(super.list(queryWrapper)!=null && super.list(queryWrapper).size()>0){
                 Organization retData = super.list(queryWrapper).get(0);

                 return retData;
             }
             count--;
             Thread.sleep(10);
         }
         throw new BizException("-1",false);
     } catch (Exception e) {
         e.printStackTrace();
         throw new BizException("-1",e,false);
     }

  }
/**
   * M1执行底层框架公共字段推送内存（特殊方法）[4519]
   * gen by moon at 10/16/2022, 11:08:26 AM
   */
 @Trace(operationName = "M1执行底层框架公共字段推送内存（特殊方法）")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  Organization  implementBottomPublicFieldPushMemory() {
     return null; //todo ruizhe dong
  }
/**
   * M1批量查询组织测试[4874]
   * gen by moon at 10/16/2022, 11:08:26 AM
   */
 @Trace(operationName = "M1批量查询组织测试")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<Organization>  batchQueryOrgTestData(Organization organization) {
     
        try {
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(organization);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Organization.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Organization.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Organization.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<Organization>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
/**
   * M1批量删组织测试[4875]
   * gen by moon at 10/16/2022, 11:08:27 AM
   */
 @Trace(operationName = "M1批量删组织测试")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchDeleteOrgTestData(List<String> reqDto) {
     
          if(reqDto==null || reqDto.size()==0){
            return false;
          }
          
          return super.removeByIds(reqDto);
  }
}
